package com.climber.android.commonres.widget

import android.content.Context
import android.text.TextUtils
import android.util.AttributeSet
import android.util.TypedValue
import android.view.Gravity
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import androidx.core.content.ContextCompat
import com.climber.android.commonres.R

/**
 * Created by mikes on 2018/5/3.
 */
class CommonRowItemView : LinearLayout {
    private lateinit var tv_left: TextView
    private lateinit var tv_right: TextView
    private lateinit var iv_left_src: ImageView
    private lateinit var iv_right_src: ImageView
    private lateinit var v_split: View
    private lateinit var vDot: View

    companion object{
        fun dip2Px(context: Context, dip : Int) : Int{
            val density = context.resources.displayMetrics.density
            return (dip * density + 0.5f).toInt()
        }

        fun sp2px(context: Context, sp : Int) : Int{
            return (TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, sp.toFloat(), context.resources.displayMetrics) + 0.5f).toInt()
        }
    }

    constructor(context: Context, attrs: AttributeSet) : super(context, attrs) {
        init(attrs)
    }

    constructor(context: Context, attrs: AttributeSet, defStyleAttr: Int) : super(context, attrs, defStyleAttr) {
        init(attrs)
    }

    private fun init(attrs: AttributeSet) {
        View.inflate(context, R.layout.com_view_row_item, this)

        orientation = VERTICAL

        tv_left = findViewById(R.id.tv_left)
        tv_right = findViewById(R.id.tv_right)
        iv_left_src = findViewById(R.id.iv_left_src)
        iv_right_src = findViewById(R.id.iv_right_src)
        v_split = findViewById(R.id.v_split)
        vDot = findViewById(R.id.vDot)

        val row_root = findViewById<View>(R.id.row_root)


        val a = context.obtainStyledAttributes(attrs,
                R.styleable.CommonRowItemView)

        a.getDimensionPixelSize(R.styleable.CommonRowItemView_cnvRowHeight, dip2Px(context, 50)).run {
            row_root.layoutParams = LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, this)
        }

        a.getResourceId(R.styleable.CommonRowItemView_cnvLeftSrc, 0).run {
            if(this != 0){
                iv_left_src.setImageResource(this)
            } else{
                iv_left_src.visibility = View.GONE
            }
        }

        a.getString(R.styleable.CommonRowItemView_cnvLeftText).run {
            if (!TextUtils.isEmpty(this)) {
                tv_left.text = this
            }
        }

        a.getDimensionPixelSize(R.styleable.CommonRowItemView_cnvLeftTextSize, sp2px(context, 16)).run {
            tv_left.setTextSize(TypedValue.COMPLEX_UNIT_PX,this.toFloat())
        }

        a.getColor(R.styleable.CommonRowItemView_cnvLeftTextColor, ContextCompat.getColor(context, R.color.common_black)).run {
            tv_left.setTextColor(this)
        }

        a.getString(R.styleable.CommonRowItemView_cnvRightText).run {
            if (!TextUtils.isEmpty(this)) {
                tv_right.text = this
            }
        }

        a.getDimensionPixelSize(R.styleable.CommonRowItemView_cnvRightTextSize, sp2px(context, 14)).run {
            tv_right.setTextSize(TypedValue.COMPLEX_UNIT_PX,this.toFloat())
        }

        a.getColor(R.styleable.CommonRowItemView_cnvRightTextColor, ContextCompat.getColor(context, R.color.common_text_secondary)).run {
            tv_right.setTextColor(this)
        }

        a.getInt(R.styleable.CommonRowItemView_cnvRightTextGravity, 0).run {
            when(this){
                1 -> tv_right.gravity = Gravity.CENTER
                else ->  tv_right.gravity = Gravity.RIGHT
            }
        }

        a.getResourceId(R.styleable.CommonRowItemView_cnvRightSrc, R.drawable.com_ic_arrow_right_small_grey).run {
            iv_right_src.setImageResource(this)
        }

        a.getBoolean(R.styleable.CommonRowItemView_cnvRightSrcShown, true).run {
            iv_right_src.visibility = if (this) View.VISIBLE else View.GONE
        }

        a.getBoolean(R.styleable.CommonRowItemView_cnvSplitShown, true).run {
            v_split.visibility = if (this) View.VISIBLE else View.INVISIBLE
        }

        a.getBoolean(R.styleable.CommonRowItemView_cnvDotShown, false).run {
            vDot.visibility = if(this) View.VISIBLE else View.GONE
        }

        a.recycle()
    }

    fun updateItem(leftText: String? = null, rightText: String? = null) {
        if (leftText != null) {
            tv_left.text = leftText
        }
        if (rightText != null) {
            tv_right.text = rightText
        }
    }

    fun updateRightItemTextGravity(gravity : Int){
        tv_right.gravity = gravity
    }

    fun toggleRedDot(visibility: Boolean) {
        vDot.visibility = if (visibility) View.VISIBLE else View.INVISIBLE
    }
}